
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>第七章 数据库迁移 Migrate · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="8tabel2model.html" />
    
    
    <link rel="prev" href="6生命周期函数.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="1.写在前面的话.html">
            
                <a href="1.写在前面的话.html">
            
                    
                    写在前面的话
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="2.搭建环境.html">
            
                <a href="2.搭建环境.html">
            
                    
                    第一章 搭建环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    第二章 模型定义
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="3.sequelize 实例变量.html">
            
                <a href="3.sequelize 实例变量.html">
            
                    
                    2.1 实例变量方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="4.定义一个模型.html">
            
                <a href="4.定义一个模型.html">
            
                    
                    2.2 定义一个模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="5.模型字段类型.html">
            
                <a href="5.模型字段类型.html">
            
                    
                    2.3 模型字段类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="6.导入与配置.html">
            
                <a href="6.导入与配置.html">
            
                    
                    2.3 导入与配置
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="3模型关系.html">
            
                <a href="3模型关系.html">
            
                    
                    第三章 模型关系
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="3.1属于.html">
            
                <a href="3.1属于.html">
            
                    
                    3.1 属于
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="3.2包含.html">
            
                <a href="3.2包含.html">
            
                    
                    3.2 包含
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="3.3一对多.html">
            
                <a href="3.3一对多.html">
            
                    
                    3.3 一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="3.4多对多.html">
            
                <a href="3.4多对多.html">
            
                    
                    3.4 多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="3.5多态一对多.html">
            
                <a href="3.5多态一对多.html">
            
                    
                    3.5 多态一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="3.6多态多对多.html">
            
                <a href="3.6多态多对多.html">
            
                    
                    3.6 多态多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="3.7关系嵌套.html">
            
                <a href="3.7关系嵌套.html">
            
                    
                    3.7 关系嵌套
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="4模型查询.html">
            
                <a href="4模型查询.html">
            
                    
                    第四章 模型查询
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="5查询范围.html">
            
                <a href="5查询范围.html">
            
                    
                    第五章 查询范围 Scope (预定义查询条件)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="6生命周期函数.html">
            
                <a href="6生命周期函数.html">
            
                    
                    第六章 生命周期函数 Hook
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.9" data-path="7migrate.html">
            
                <a href="7migrate.html">
            
                    
                    第七章 数据库迁移 Migrate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="8tabel2model.html">
            
                <a href="8tabel2model.html">
            
                    
                    第八章 根据 Table 生成 Model
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >第七章 数据库迁移 Migrate</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="migrate">Migrate</h1>
<p>&#x9996;&#x5148;&#x5B89;&#x88C5;&#x4EE5;&#x4E0B;&#x5DE5;&#x5177; &#xFF08;&#x5DE5;&#x5177;&#x4E0D;&#x63D0;&#x4F9B; ts &#x652F;&#x6301;&#xFF09;</p>
<pre><code class="lang-ts">npm install -g sequelize-cli
</code></pre>
<pre><code class="lang-bash">mkdir models &amp;&amp; <span class="hljs-built_in">cd</span> models
npm init -y
npm i sequelize mysql2 -S
</code></pre>
<p>&#x5728;&#x5B89;&#x88C5;&#x4E86; sequelize &#x7684; node &#x9879;&#x76EE;&#x4E2D;&#x8FD0;&#x884C;&#x4EE5;&#x4E0B;&#xFF0C;&#x4FBF;&#x53EF;&#x770B;&#x5230;&#x5982;&#x4E0B;&#x4FE1;&#x606F;&#x3002;</p>
<pre><code class="lang-bash">$ sequelize

Sequelize [Node: 8.1.2, CLI: 2.7.0, ORM: 4.2.0]

Usage
  sequelize [task] [OPTIONS...]
Available tasks
  db:migrate             Run pending migrations.
  db:migrate:old_schema  Update legacy migration table
  db:migrate:status      List the status of all migrations
  db:migrate:undo        Reverts a migration.
  db:migrate:undo:all    Revert all migrations ran.
  db:seed                Run specified seeder.
  db:seed:all            Run every seeder.
  db:seed:undo           Deletes data from the database.
  db:seed:undo:all       Deletes data from the database.
  <span class="hljs-built_in">help</span>                   Display this <span class="hljs-built_in">help</span> text. Aliases: h
  init                   Initializes the project. [init:config, init:migrations, init:seeders, init:models]
  init:config            Initializes the configuration.
  init:migrations        Initializes the migrations.
  init:models            Initializes the models.
  init:seeders           Initializes the seeders.
  migration:create       Generates a new migration file. Aliases: migration:generate
  model:create           Generates a model and its migration. Aliases: model:generate
  seed:create            Generates a new seed file. Aliases: seed:generate
  version                Prints the version number. Aliases: v
Available manuals
  <span class="hljs-built_in">help</span>:db:migrate             The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize db:migrate&quot;</span>.
  <span class="hljs-built_in">help</span>:db:migrate:old_schema  The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize db:migrate:old_schema&quot;</span>.
  <span class="hljs-built_in">help</span>:db:migrate:status      The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize db:migrate:status&quot;</span>.
  <span class="hljs-built_in">help</span>:db:migrate:undo        The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize db:migrate:undo&quot;</span>.
  <span class="hljs-built_in">help</span>:db:migrate:undo:all    The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize db:migrate:undo:all&quot;</span>.
  <span class="hljs-built_in">help</span>:db:seed                The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize db:seed&quot;</span>.
  <span class="hljs-built_in">help</span>:db:seed:all            The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize db:seed:all&quot;</span>.
  <span class="hljs-built_in">help</span>:db:seed:undo           The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize db:seed:undo&quot;</span>.
  <span class="hljs-built_in">help</span>:db:seed:undo:all       The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize db:seed:undo:all&quot;</span>.
  <span class="hljs-built_in">help</span>:init                   The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize init&quot;</span>.
  <span class="hljs-built_in">help</span>:init:config            The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize init:config&quot;</span>.
  <span class="hljs-built_in">help</span>:init:migrations        The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize init:migrations&quot;</span>.
  <span class="hljs-built_in">help</span>:init:models            The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize init:models&quot;</span>.
  <span class="hljs-built_in">help</span>:init:seeders           The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize init:seeders&quot;</span>.
  <span class="hljs-built_in">help</span>:migration:create       The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize migration:create&quot;</span>.
  <span class="hljs-built_in">help</span>:model:create           The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize model:create&quot;</span>.
  <span class="hljs-built_in">help</span>:seed:create            The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize seed:create&quot;</span>.
  <span class="hljs-built_in">help</span>:version                The documentation <span class="hljs-keyword">for</span> <span class="hljs-string">&quot;sequelize version&quot;</span>.
</code></pre>
<p>&#x5BF9;&#x4E8E;&#x547D;&#x4EE4;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; help:xxxx &#x6765;&#x67E5;&#x770B;&#x5E2E;&#x52A9;&#x3002;</p>
<h3 id="&#x521D;&#x59CB;&#x5316;">&#x521D;&#x59CB;&#x5316;</h3>
<p>&#x8FD0;&#x884C; <code>sequelize init</code> &#x8FDB;&#x884C;&#x521D;&#x59CB;&#x5316;&#xFF0C;&#x8BE5;&#x547D;&#x4EE4;&#x662F;&#x4EE5;&#x4E0B;&#x547D;&#x4EE4;&#x7684;&#x603B;&#x548C;&#x3002;</p>
<pre><code class="lang-bash">init:config            Initializes the configuration.
init:migrations        Initializes the migrations.
init:models            Initializes the models.
init:seeders           Initializes the seeders.
</code></pre>
<p>&#x6B64;&#x65F6;&#x6211;&#x4EEC;&#x4F1A;&#x5F97;&#x5230;&#x51E0;&#x4E2A;&#x6587;&#x4EF6;&#x5939; config / migrations / models/ seeders</p>
<p>config &#x662F; sequelize &#x7684;&#x914D;&#x7F6E;&#x76EE;&#x5F55;&#xFF0C;&#x6BD4;&#x5982;&#x6570;&#x636E;&#x5E93;&#x5730;&#x5740;&#x7B49;&#x7B49;&#xFF0C;&#x6709;&#x597D;&#x51E0;&#x4E2A;&#x73AF;&#x5883;&#x7684;&#x914D;&#x7F6E;&#x9879;&#x3002;
migrations &#x662F;&#x901A;&#x8FC7; js &#x6587;&#x4EF6;&#x91CC;&#x9762;&#x5B9A;&#x4E49;&#x597D;&#x7684;&#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;&#x3002;&#x4FEE;&#x6539;&#x6570;&#x636E;&#x5E93;&#x8868;&#x7684;&#x6587;&#x4EF6;&#x3002;
models &#x5C31;&#x662F;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x5B9A;&#x4E49;&#x6A21;&#x578B;&#x7684;&#x5730;&#x65B9;
seeders &#x5C31;&#x662F;&#x586B;&#x5145;&#x5047;&#x6570;&#x636E;&#x7684;&#x5730;&#x65B9;</p>
<h3 id="&#x67E5;&#x770B;&#x5982;&#x4F55;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6A21;&#x578B;">&#x67E5;&#x770B;&#x5982;&#x4F55;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6A21;&#x578B;</h3>
<p>&#x8FD0;&#x884C;&#x4E00;&#x4E0B; sequelize help:model:create &#x547D;&#x4EE4;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x5982;&#x4E0B;&#x4FE1;&#x606F;&#x3002;</p>
<pre><code>$ sequelize help:model:create

Sequelize [Node: 8.1.2, CLI: 2.7.0, ORM: 4.2.0]

Loaded configuration file &quot;config/config.json&quot;.
Using environment &quot;development&quot;.
COMMANDS
    sequelize model:create   -- Generates a model and its migration.
    sequelize model:generate -- Generates a model and its migration.
DESCRIPTION
    This task generates a model file and its respective migration.
    It is necessary to specify the name of the new model as well as
    the model&apos;s attributes.

    The attributes can be specified as in the following (and semantically equal) examples:

    sequelize model:create --name User --attributes first_name:string,last_name:string,bio:text
    sequelize model:create --name User --attributes &apos;first_name:string last_name:string bio:text&apos;
    sequelize model:create --name User --attributes &apos;first_name:string, last_name:string, bio:text&apos;

    This command will generate a new migration and model definition:

    // the model file
    // located under models/user.js
    &apos;use strict&apos;;
    module.exports = function(sequelize, DataTypes) {
      var User = sequelize.define(&apos;User&apos;, {
        first_name: DataTypes.STRING,
        last_name: DataTypes.STRING,
        bio: DataTypes.TEXT
      }, {
        classMethods: {
          associate: function(models) {
            // associations can be defined here
          }
        }
      });
      return User;
    };

    // the migration file
    // located under migrations/20170628070436-create-user.js
    &apos;use strict&apos;;
    module.exports = {
      up: function(queryInterface, Sequelize) {
        return queryInterface.createTable(&apos;Users&apos;, {
          id: {
            allowNull: false,
            autoIncrement: true,
            primaryKey: true,
            type: Sequelize.INTEGER
          },
          first_name: {
            type: Sequelize.STRING
          },
          last_name: {
            type: Sequelize.STRING
          },
          bio: {
            type: Sequelize.TEXT
          },
          createdAt: {
            allowNull: false,
            type: Sequelize.DATE
          },
          updatedAt: {
            allowNull: false,
            type: Sequelize.DATE
          }
        });
      },
      down: function(queryInterface, Sequelize) {
        return queryInterface.dropTable(&apos;Users&apos;);
      }
    };
</code></pre><p>&#x7D27;&#x63A5;&#x7740;&#x6211;&#x4EEC;&#x518D;&#x8FD0;&#x884C;&#x4E00;&#x4E0B; </p>
<pre><code class="lang-ts">sequelize model:create --name User --attributes first_name:<span class="hljs-built_in">string</span>,last_name:<span class="hljs-built_in">string</span>,bio:text
sequelize model:create --name Book --attributes title:<span class="hljs-built_in">string</span>,authorId:integer,desc:text
</code></pre>
<p>&#x518D;&#x6765;&#x770B;&#x770B; models &#x6587;&#x4EF6;&#x5939;&#x548C; migrations &#x6587;&#x4EF6;&#x5939;&#x5C31;&#x4F1A;&#x53D1;&#x73B0;&#x51FA;&#x73B0;&#x4E86;&#x4E00;&#x4E9B;&#x6587;&#x4EF6;&#x3002;</p>
<p>&#x5728; <code>migrations/xxxxx-create-user.js</code> &#x6587;&#x4EF6;&#x91CC;&#x9762;&#x4F1A;&#x770B;&#x5230;&#xFF0C;<code>queryInterface.createTable</code>&#x5F88;&#x660E;&#x663E;&#x5C31;&#x662F;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x8868;&#x800C; <code>queryInterface.dropTable</code>&#x5C31;&#x662F;&#x5220;&#x9664;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x8868;&#x3002;</p>
<p>queryInterface &#x6C14;&#x6B7B;&#x5728; d.ts &#x6587;&#x4EF6;&#x91CC;&#x9762;&#x53EF;&#x4EE5;&#x627E;&#x5230;&#xFF0C;&#x5728; <code>4206 - 4329</code> &#x884C;&#xFF0C;&#x5927;&#x5BB6;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x67E5;&#x627E;&#x4ED6;&#x80FD;&#x4F7F;&#x7528;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<h3 id="&#x5B9A;&#x4E49;&#x4E00;&#x4E0B;&#x5173;&#x7CFB;">&#x5B9A;&#x4E49;&#x4E00;&#x4E0B;&#x5173;&#x7CFB;</h3>
<p>models/user.js</p>
<pre><code class="lang-js">    classMethods: {
      associate: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">models</span>) </span>{
        <span class="hljs-comment">// associations can be defined here</span>
        models.User.hasMany(models.Book)
        models.Book.belongsTo(models.User);
      }
    }
</code></pre>
<h3 id="&#x89E3;&#x51B3;&#x65E0;&#x6CD5;&#x5173;&#x8054;&#x7684;&#x95EE;&#x9898;">&#x89E3;&#x51B3;&#x65E0;&#x6CD5;&#x5173;&#x8054;&#x7684;&#x95EE;&#x9898;</h3>
<p>&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x6765;&#x6D4B;&#x8BD5;&#x4E00;&#x4E0B;&#x65B9;&#x6CD5;</p>
<p>&#x5728;&#x9879;&#x76EE;&#x6839;&#x76EE;&#x5F55;&#x4E0B;&#x9762;&#x65B0;&#x5EFA; index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> db = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./models&apos;</span>);

<span class="hljs-built_in">console</span>.dir(db.sequelize.models.User)
</code></pre>
<p>&#x8FD0;&#x884C;&#x4E00;&#x4E0B;&#xFF0C;&#x4F1A;&#x53D1;&#x73B0; User &#x5E76;&#x6CA1;&#x6709;&#x4E0E; Book &#x5EFA;&#x7ACB;&#x8054;&#x7CFB;&#xFF0C;&#x8FD9;&#x662F;&#x4EC0;&#x4E48;&#x60C5;&#x51B5;&#x5462;&#xFF1F;&#x4F46;&#x662F;&#x786E;&#x5B9E;&#x53C8;&#x662F;&#x5728;&#x5BF9;&#x8C61;&#x91CC;&#x9762;&#x770B;&#x5230;&#x4E86;&#x6709;<code>classMethods</code> &#x8FD9;&#x4E2A;&#x4E1C;&#x897F;&#x3002;</p>
<p>&#x7136;&#x540E;&#x6211;&#x4EEC;&#x518D;&#x6765;&#x5230; models/index.js &#x4FEE;&#x6539;&#x4E00;&#x4E0B; 27 &#x884C;&#x3001;</p>
<pre><code class="lang-js"><span class="hljs-built_in">Object</span>.keys(db).forEach(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">modelName</span>) </span>{
  <span class="hljs-built_in">console</span>.dir(db[modelName].options.classMethods.associate);
  <span class="hljs-built_in">console</span>.log(db[modelName].associate);
  <span class="hljs-keyword">if</span> (db[modelName].associate) {
    db[modelName].associate(db);
  }
});
</code></pre>
<p>&#x6253;&#x5370;&#x5982;&#x4E0B;&#xFF08;index.js &#x662F;&#x9879;&#x76EE;&#x8DDF;&#x76EE;&#x5F55;&#x4E0B;&#x9762;&#x7684;&#xFF09;</p>
<pre><code>$ node index.js
[Function: associate]
undefined
[Function: associate]
undefined
</code></pre><p>&#x8FD9;&#x5F88;&#x660E;&#x663E;&#xFF0C;classMethods &#x4E0A;&#x9762;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x6CA1;&#x6709;&#x4EE3;&#x7406;&#x5230;&#x5BF9;&#x8C61;&#x4E0A;&#x9762;&#x53BB;&#x3002;&#x800C;&#x4E14;&#x5728;&#x4E4B;&#x524D;&#x4E5F;&#x63D0;&#x5230;&#x8FC7; v4 &#x7248;&#x672C;&#x597D;&#x50CF;&#x4E0D;&#x518D;&#x652F;&#x6301; &#x8FD9;&#x79CD;&#x505A;&#x6CD5;&#xFF0C;&#x6240;&#x4EE5;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x4FEE;&#x6539;&#x4E00;&#x4E0B;&#x3002;</p>
<pre><code>Object.keys(db).forEach(function(modelName) {
  const associate = db[modelName].options.classMethods.associate;
  associate &amp;&amp; associate(db);
});
</code></pre><p>&#x4E4B;&#x540E;&#x518D;&#x6B21;&#x8FD0;&#x884C;</p>
<pre><code class="lang-bash">$ node index.js | grep Book
     models: { Book: [Object], User: [Circular] },
   { Books:
        as: <span class="hljs-string">&apos;Books&apos;</span>,
        associationAccessor: <span class="hljs-string">&apos;Books&apos;</span>,
</code></pre>
<p>&#x5F88;&#x660E;&#x663E;&#x8FD9;&#x4E00;&#x6B21;&#x5C31;&#x6210;&#x529F;&#x7684;&#x8FDB;&#x884C;&#x4E86;&#x5173;&#x8054;&#x3002;</p>
<h3 id="&#x518D;&#x6B21;&#x4FEE;&#x6539;&#x5E76;&#xFF0C;&#x4FDD;&#x5B58;&#x4E00;&#x4E0B;&#x8054;&#x7CFB;">&#x518D;&#x6B21;&#x4FEE;&#x6539;&#x5E76;&#xFF0C;&#x4FDD;&#x5B58;&#x4E00;&#x4E0B;&#x8054;&#x7CFB;</h3>
<pre><code class="lang-js">associate: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">models</span>) </span>{
   <span class="hljs-comment">// associations can be defined here</span>
   models.User.Book = models.User.hasOne(models.Book, {
     foreignKey: <span class="hljs-string">&apos;authorId&apos;</span>
   });
   models.Book.User = models.Book.belongsTo(models.User, {
     foreignKey: <span class="hljs-string">&apos;authorId&apos;</span>
   });
 }
</code></pre>
<h3 id="seeder-&#x586B;&#x5145;">seeder &#x586B;&#x5145;</h3>
<p>&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x586B;&#x5145;&#x6587;&#x4EF6;</p>
<pre><code class="lang-bash">sequelize seed:create --name create_users
</code></pre>
<p>&#x5B89;&#x88C5;&#x5047;&#x6570;&#x636E;&#x751F;&#x6210;&#x5DE5;&#x5177;</p>
<pre><code class="lang-bash">npm i -D faker
</code></pre>
<p>&#x5047;&#x6570;&#x636E;&#x751F;&#x6210;&#x5DE5;&#x5177;&#x652F;&#x6301;&#x7684;&#x914D;&#x7F6E;<a href="https://cdn.rawgit.com/Marak/faker.js/master/examples/browser/index.html#lorem" target="_blank">&#x8BF7;&#x70B9;&#x8FD9;&#x91CC;</a></p>
<p>&#x4FEE;&#x6539;&#x6587;&#x4EF6;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-meta">&apos;use strict&apos;</span>;

<span class="hljs-keyword">const</span> faker = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;faker&apos;</span>);
faker.locale = <span class="hljs-string">&quot;zh_CN&quot;</span>;

<span class="hljs-keyword">const</span> db = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;../models&apos;</span>)

<span class="hljs-built_in">module</span>.exports = {
  up: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">queryInterface, Sequelize</span>) </span>{
    <span class="hljs-comment">/*
      Add altering commands here.
      Return a promise to correctly handle asynchronicity.

      Example:
      return queryInterface.bulkInsert(&apos;Person&apos;, [{
        name: &apos;John Doe&apos;,
        isBetaMember: false
      }], {});
    */</span>

    <span class="hljs-keyword">const</span> Users = <span class="hljs-built_in">Array</span>.from({length:<span class="hljs-number">20</span>}, () =&gt; {
      <span class="hljs-keyword">return</span> {
        first_name: faker.name.firstName(),
        last_name: faker.name.lastName(),
        bio: faker.name.jobTitle(),
        Book: {
          title: faker.lorem.word(),
          desc: faker.lorem.sentence()
        }
      }
    });
    <span class="hljs-keyword">const</span> User = db.sequelize.models.User;
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Promise</span>.all(Users.map((item) =&gt; {
      <span class="hljs-keyword">return</span> User.create(item, {
        include: [ User.Book ]
      });
    }))
  },

  down: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">queryInterface, Sequelize</span>) </span>{
    <span class="hljs-comment">/*
      Add reverting commands here.
      Return a promise to correctly handle asynchronicity.

      Example:
      return queryInterface.bulkDelete(&apos;Person&apos;, null, {});
    */</span>
   <span class="hljs-keyword">return</span> queryInterface.bulkDelete(<span class="hljs-string">&apos;Person&apos;</span>, <span class="hljs-literal">null</span>, {});
  }
};
</code></pre>
<blockquote>
<p>&#x5BF9;&#x4E8E;&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#xFF0C;&#x89C9;&#x5F97;&#x5F88;&#x96BE;&#x7684;&#x81EA;&#x5DF1;&#x7528;&#x6700;&#x7B80;&#x5355;&#x7684; queryInterface.bulkInsert &#x53BB;&#x63D2;&#x5165;&#x6570;&#x636E;&#x5C31;&#x884C;&#x4E86;&#x3002;
&#x5D4C;&#x5957;&#x7684;&#x8FD9;&#x79CD;&#x5F62;&#x5F0F;&#xFF0C;&#x6211;&#x627E;&#x4E86;&#x4E00;&#x4E0B; API &#x53D1;&#x73B0;&#x53EA;&#x6709; create &#x652F;&#x6301;&#x3002;</p>
</blockquote>
<p>&#x8FD0;&#x884C;&#x547D;&#x4EE4; <code>sequelize db:migrate:status</code> &#x67E5;&#x770B;&#x5F53;&#x524D; &#x6570;&#x636E;&#x5E93;&#x8868;&#x521B;&#x5EFA;&#x7684;&#x60C5;&#x51B5;</p>
<pre><code>down 20170628070615-create-user.js
down 20170628070751-create-book.js
</code></pre><p>&#x8FD9;&#x91CC;&#x8868;&#x793A;&#x6211;&#x4EEC;&#x6CA1;&#x6709;&#x521B;&#x5EFA;&#x8868;&#x3002;&#x521B;&#x5EFA;&#x4E86;&#x4E4B;&#x540E;&#x4F1A;&#x662F; <code>up</code> &#x72B6;&#x6001;&#x3002;</p>
<p>&#x8FD0;&#x884C; <code>sequelize db:migrate</code> </p>
<pre><code>$ sequelize db:migrate

Sequelize [Node: 8.1.2, CLI: 2.7.0, ORM: 4.2.0]

Loaded configuration file &quot;config/config.json&quot;.
Using environment &quot;development&quot;.
== 20170628070615-create-user: migrating =======
== 20170628070615-create-user: migrated (0.100s)
== 20170628070751-create-book: migrating =======
== 20170628070751-create-book: migrated (0.261s)
=
</code></pre><p>&#x6B64;&#x65F6;&#x6570;&#x636E;&#x5E93;&#x91CC;&#x9762;&#x4EE5;&#x53CA;&#x6709;&#x4E86;&#x8868;&#x4E86;&#x3002;&#x800C;&#x4E14;&#x591A;&#x4E86;&#x4E00;&#x4E2A;&#x8868;<code>SequelizeMeta</code> &#x8FD9;&#x4E2A;&#x662F;&#x7528;&#x6765;&#x7EE7;&#x7EED;&#x8FC1;&#x79FB;&#x7684;&#x8868;&#x3002;</p>
<pre><code>$ sequelize db:migrate:undo:all

Sequelize [Node: 8.1.2, CLI: 2.7.0, ORM: 4.2.0]

Loaded configuration file &quot;config/config.json&quot;.
Using environment &quot;development&quot;.
== 20170628070751-create-book: reverting =======
== 20170628070751-create-book: reverted (0.017s)
== 20170628070615-create-user: reverting =======
== 20170628070615-create-user: reverted (0.008s)
</code></pre><p><code>sequelize db:migrate:undo:all</code> &#x53EF;&#x4EE5;&#x64A4;&#x9500;&#x521A;&#x521A;&#x6240;&#x8FC1;&#x79FB;&#x7684;&#x6570;&#x636E;&#x8868;&#xFF0C;&#x524D;&#x63D0;&#x662F;&#x4F60;&#x7684; migarations &#x6587;&#x4EF6;&#x91CC;&#x9762;&#x6709;&#x5199;&#x8FD9;&#x4E2A; down &#x65B9;&#x6CD5;&#x3002;&#x8FD9;&#x79CD;&#x64CD;&#x4F5C;&#x88AB;&#x79F0;&#x4E3A;&#x56DE;&#x6EDA;&#x3002;<code>undo:all</code> &#x662F;&#x64A4;&#x9500;&#x5168;&#x90E8;&#xFF0C;&#x800C;<code>undo</code>&#x5C31;&#x662F;&#x64A4;&#x9500;&#x4E00;&#x6B65;&#x3002;</p>
<pre><code>down: function(queryInterface, Sequelize) {
    return queryInterface.dropTable(&apos;Books&apos;);
}
</code></pre><p>&#x63A5;&#x4E0B;&#x6765;&#x7EE7;&#x7EED;&#x8FD0;&#x884C;&#x547D;&#x4EE4;</p>
<pre><code>sequelize db:migrate
sequelize db:seed:all
</code></pre><p>&#x4FBF;&#x53EF;&#x5728;&#x6570;&#x636E;&#x5E93;&#x91CC;&#x9762;&#x770B;&#x5230;&#x6570;&#x636E;&#xFF0C;&#x4E0D;&#x8FC7;&#x6B64;&#x64CD;&#x4F5C;&#x4E5F;&#x662F;&#x53EF;&#x4EE5;&#x56DE;&#x6EDA;&#x7684;&#xFF0C;&#x901A;&#x8FC7;<code>seed:undo:all</code>&#x5373;&#x53EF;&#x3002;&#x5F53;&#x7136;&#x524D;&#x63D0;&#x662F;&#x4F60;&#x5728;&#x4EE3;&#x7801;&#x91CC;&#x9762;&#x4EE5;&#x53CA;&#x5B9A;&#x4E49;&#x4E86;&#x5982;&#x4F55;&#x56DE;&#x6EDA;&#x3002;&#x4E5F;&#x5C31;&#x662F;<code>down</code>&#x65B9;&#x6CD5;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="6生命周期函数.html" class="navigation navigation-prev " aria-label="Previous page: 第六章 生命周期函数 Hook">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="8tabel2model.html" class="navigation navigation-next " aria-label="Next page: 第八章 根据 Table 生成 Model">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第七章 数据库迁移 Migrate","level":"1.9","depth":1,"next":{"title":"第八章 根据 Table 生成 Model","level":"1.10","depth":1,"path":"document/8tabel2model.md","ref":"./document/8tabel2model.md","articles":[]},"previous":{"title":"第六章 生命周期函数 Hook","level":"1.8","depth":1,"path":"document/6生命周期函数.md","ref":"./document/6生命周期函数.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"document/7migrate.md","mtime":"2017-06-28T08:28:35.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-06-28T08:29:37.466Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

